package com.atguigu.tingshu.payment.service.impl;

import com.atguigu.tingshu.common.constant.SystemConstant;
import com.atguigu.tingshu.common.rabbit.constant.MqConst;
import com.atguigu.tingshu.common.rabbit.service.RabbitService;
import com.atguigu.tingshu.model.payment.PaymentInfo;
import com.atguigu.tingshu.payment.mapper.PaymentInfoMapper;
import com.atguigu.tingshu.payment.service.PaymentInfoService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wechat.pay.java.service.payments.model.Transaction;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Date;

@Service
@SuppressWarnings({"all"})
public class PaymentInfoServiceImpl extends ServiceImpl<PaymentInfoMapper, PaymentInfo> implements PaymentInfoService {
    @Resource
    private  RabbitService rabbitService;

    public PaymentInfoServiceImpl(RabbitService rabbitService) {
        this.rabbitService = rabbitService;
    }

    @Override
    public void savePaymentInfo(Long userId, String orderNo, String paymentType, BigDecimal totalAmount) {
        //  创建一个paymentInfo 对象;
        PaymentInfo paymentInfo = new PaymentInfo();
        paymentInfo.setUserId(userId);
        paymentInfo.setPaymentType(paymentType);
        paymentInfo.setOrderNo(orderNo);
        paymentInfo.setPayWay(SystemConstant.ORDER_PAY_WAY_WEIXIN);
        paymentInfo.setAmount(totalAmount);
        paymentInfo.setContent(paymentType.equals(SystemConstant.PAYMENT_TYPE_ORDER)?"微信支付":"微信充值");
        paymentInfo.setPaymentStatus(SystemConstant.PAYMENT_STATUS_UNPAID);
        //  保存数据;
        this.save(paymentInfo);
    }

    @Override
    public void updatePaymentStatus(Transaction result) {
        PaymentInfo paymentInfo = this.getOne(new LambdaQueryWrapper<PaymentInfo>().eq(PaymentInfo::getOrderNo, result.getOutTradeNo()));
        if (paymentInfo != null && paymentInfo.getPaymentStatus().equals(SystemConstant.PAYMENT_STATUS_PAID)){
            // 如果支付状态已经是支付成功后就不用进行修改  直接返回就行
            return;
        }
        // 反之 则进行修改
        paymentInfo.setPaymentStatus(SystemConstant.PAYMENT_STATUS_PAID);
        paymentInfo.setOrderNo(result.getOutTradeNo());
        paymentInfo.setOrderNo(result.getTransactionId());
        paymentInfo.setCallbackTime(new Date());
        paymentInfo.setCallbackContent(result.toString());
        this.updateById(paymentInfo);
        //  交易成功后  发消息进行记录
        // 判断是保存订单 还是保存充值
        String routing = paymentInfo.getPaymentType().equals(SystemConstant.PAYMENT_TYPE_ORDER)? MqConst.ROUTING_ORDER_PAY_SUCCESS : MqConst.ROUTING_RECHARGE_PAY_SUCCESS;
        rabbitService.sendMessage(MqConst.EXCHANGE_ORDER,routing,paymentInfo.getOrderNo());
    }

    @Override
    public boolean closePayment(String orderNo) {
        PaymentInfo paymentInfo = new PaymentInfo();
        paymentInfo.setPaymentStatus(SystemConstant.PAYMENT_STATUS_CANCEL);
        //什么时候更新
        boolean update = this.update(paymentInfo, new LambdaQueryWrapper<PaymentInfo>().eq(PaymentInfo::getOrderNo, orderNo));
        return update;
    }
}
